package io.renren.modules.sys.web_api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.Stringutils;
import io.renren.modules.sys.back.entity.*;
import io.renren.modules.sys.back.service.*;
import io.renren.modules.sys.back.service.impl.BtCtYingyongDetailServiceImpl;
import io.renren.modules.sys.web_api.service.ProductPageService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("productPageService")
public class ProductPageServiceImpl implements ProductPageService{
    @Autowired
    private BtCategroyManageService btCategroyManageService;

    @Override
    public List<Map<String,Object>> getSubProductErJi(Long pid){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        List<BtCategroyManageEntity> ctbeanlist = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",pid));
        for(BtCategroyManageEntity sb:ctbeanlist){
            Map<String,Object> data = new HashMap<>();
            data.put("name",sb.getName());
            data.put("link","/api/product/subcatag/"+sb.getId());
            data.put("erjiid",sb.getId());
            datalist.add(data);
        }
        return datalist;
    }
    @Override
    public List<Map<String,Object>> getSubProductList(Long pid){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        List<BtCategroyManageEntity> ctbeanlist = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",pid));
        for(BtCategroyManageEntity sb:ctbeanlist){
           List<BtCtBaseEntity> ctbb = btCtBaseService.selectList(new EntityWrapper<BtCtBaseEntity>().eq("fid",sb.getId()));
           for(BtCtBaseEntity ssb :ctbb){
               Map<String,Object> data = new HashMap<>();
               BtCtGaishuEntity btCtGaishuEntity= btCtGaishuService.selectById(ssb.getGsid());
               if(btCtGaishuEntity==null) continue;
               if(!Stringutils.IsNULLorEmpty(btCtGaishuEntity.getContent()))continue;
               String tmp = btCtGaishuEntity.getContent();
               if(!tmp.equals("")){
                   data.put("name",ssb.getName());
                   data.put("img",ssb.getImg());
                   data.put("link","/api/product/detail/"+ssb.getId());
                   Document doc = Jsoup.parse(tmp);
                   Elements subimg = doc.getElementsByTag("p");
                   if(subimg!=null&&subimg.size()>0) {
                       data.put("content",subimg.get(0).html());
                   }
                   datalist.add(data);
               }
           }
        }
        return datalist;
    }
    @Override
    public BtCategroyManageEntity getcatagname(Long pid){
        return btCategroyManageService.selectById(pid);
    }

    @Autowired
    private BtCtBaseService btCtBaseService;
    @Autowired
    private BtCtGaishuService btCtGaishuService;
    @Autowired
    private BtCtGongzhuoyuanliService btCtGongzhuoyuanliService;
    @Autowired
    private BtCtYingyongService btCtYingyongService;
    @Autowired
    private BtCtYingyongDetailService btCtYingyongDetailService;
    @Autowired
    private BtCtFenleiService btCtFenleiService;
    @Autowired
    private BtCtFenleiDetailService btCtFenleiDetailService;
    @Autowired
    private BtCtZhuyishixiangService btCtZhuyishixiangService;
    @Autowired
    private BtCtZhuyishixiangDetailService btCtZhuyishixiangDetailService;

    @Autowired
    private BtCtXuanxingzhinanService btCtXuanxingzhinanService;
    @Autowired
    private BtCtXuanxingzhinanDetailService btCtXuanxingzhinanDetailService;

    @Autowired
    private BtCtWeihubaoyangService btCtWeihubaoyangService;
    @Autowired
    private BtCtWeihubaoyangDetailService btCtWeihubaoyangDetailService;

    @Autowired
    private BtCtGuozhangpaichuService btCtGuozhangpaichuService;
    @Autowired
    private BtCtGuozhangpaichuDetailService btCtGuozhangpaichuDetailService;
    @Override
    public Map<String,Object> getProductDtailInfo(Long id){
        Map<String,Object> backdata = new HashMap<>();
        BtCtBaseEntity pdbean = btCtBaseService.selectById(id);
        backdata.put("naverji","产品");
        backdata.put("naverjilink","/api/product/catag/0");
        backdata.put("navwuji",pdbean.getName());
        BtCategroyManageEntity sanji = btCategroyManageService.selectById(pdbean.getFid());
        BtCategroyManageEntity erji = btCategroyManageService.selectById(sanji.getPid());
        backdata.put("navsiji",erji.getName());
        backdata.put("navsijilink","/api/product/subcatag/"+erji.getId());
        BtCategroyManageEntity yiji = btCategroyManageService.selectById(erji.getPid());
        backdata.put("navsanji",yiji.getName());
        backdata.put("navsanjilink","/api/product/catag/"+yiji.getId());
        Map<String,Object> subcontent = null;
        //概述
        subcontent = new HashMap<>();
        BtCtGaishuEntity btCtGaishuEntity = btCtGaishuService.selectById(pdbean.getGsid());
        subcontent.put("name","简介");
        subcontent.put("content",btCtGaishuEntity.getContent());
        backdata.put("gaishu",subcontent);
        //工作原理
        subcontent = new HashMap<>();
        BtCtGongzhuoyuanliEntity btCtGongzhuoyuanliEntity = btCtGongzhuoyuanliService.selectById(pdbean.getGzylid());
        if(btCtGongzhuoyuanliEntity.getContent()!=null){
            subcontent.put("name","工作原理");
            subcontent.put("content",btCtGongzhuoyuanliEntity.getContent());
            backdata.put("gongzhuyuanli",subcontent);
        }
        List<Map<String,Object>> subfenleilist=null;//条目分类
        Map<String,Object> subsubcont =null;//条目内容
        //产品应用
        if(pdbean.getYyid()!=null){
            subcontent = new HashMap<>();
            BtCtYingyongEntity btCtYingyongEntity = btCtYingyongService.selectById(pdbean.getYyid());
            if(btCtYingyongEntity!=null){
                if(btCtYingyongEntity.getFenleiid()!=null && btCtYingyongEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtYingyongEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtYingyongDetailEntity btCtYingyongDetailEntity = btCtYingyongDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtYingyongDetailEntity.getName());
                        subsubcont.put("content",btCtYingyongDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","应用");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("yingyong",subcontent);
                }else{
                    if(btCtYingyongEntity.getSummary()!=null&& btCtYingyongEntity.getSummary()!=""){
                        subcontent.put("name","应用");
                        subcontent.put("content",btCtYingyongEntity.getSummary());
                        backdata.put("yingyong",subcontent);
                    }
                }
            }
        }
        //产品分类
        if(pdbean.getFlid()!=null){
            subcontent = new HashMap<>();
            BtCtFenleiEntity btCtFenleiEntity = btCtFenleiService.selectById(pdbean.getFlid());
            if(btCtFenleiEntity!=null){
                if(btCtFenleiEntity.getFenleiid()!=null && btCtFenleiEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtFenleiEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtFenleiDetailEntity btCtFenleiDetailEntity = btCtFenleiDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtFenleiDetailEntity.getName());
                        subsubcont.put("content",btCtFenleiDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","分类");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("fenlei",subcontent);
                }else{
                    if(btCtFenleiEntity.getSummary()!=null&& btCtFenleiEntity.getSummary()!=""){
                        subcontent.put("name","分类");
                        subcontent.put("content",btCtFenleiEntity.getSummary());
                        backdata.put("fenlei",subcontent);
                    }
                }
            }
        }
        //注意事项
        if(pdbean.getZysxid()!=null){
            subcontent = new HashMap<>();
            BtCtZhuyishixiangEntity btCtZhuyishixiangEntity = btCtZhuyishixiangService.selectById(pdbean.getZysxid());
            if(btCtZhuyishixiangEntity!=null){
                if(btCtZhuyishixiangEntity.getFenleiid()!=null && btCtZhuyishixiangEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtZhuyishixiangEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtZhuyishixiangDetailEntity btCtZhuyishixiangDetailEntity = btCtZhuyishixiangDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtZhuyishixiangDetailEntity.getName());
                        subsubcont.put("content",btCtZhuyishixiangDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","注意事项");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("zhuyishixiang",subcontent);
                }else{
                    if(btCtZhuyishixiangEntity.getSummary()!=null&& btCtZhuyishixiangEntity.getSummary()!=""){
                        subcontent.put("name","注意事项");
                        subcontent.put("content",btCtZhuyishixiangEntity.getSummary());
                        backdata.put("zhuyishixiang",subcontent);
                    }
                }
            }
        }
        //选型指南
        if(pdbean.getXxznid()!=null){
            subcontent = new HashMap<>();
            BtCtXuanxingzhinanEntity btCtXuanxingzhinanEntity = btCtXuanxingzhinanService.selectById(pdbean.getXxznid());
            if(btCtXuanxingzhinanEntity!=null){
                if(btCtXuanxingzhinanEntity.getFenleiid()!=null && btCtXuanxingzhinanEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtXuanxingzhinanEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtXuanxingzhinanDetailEntity btCtXuanxingzhinanDetailEntity = btCtXuanxingzhinanDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtXuanxingzhinanDetailEntity.getName());
                        subsubcont.put("content",btCtXuanxingzhinanDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","选型指南");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("xuanxingzhinan",subcontent);
                }else{
                    if(btCtXuanxingzhinanEntity.getSummary()!=null&& btCtXuanxingzhinanEntity.getSummary()!=""){
                        subcontent.put("name","选型指南");
                        subcontent.put("content",btCtXuanxingzhinanEntity.getSummary());
                        backdata.put("xuanxingzhinan",subcontent);
                    }
                }
            }
        }
        //维护保养
        if(pdbean.getWhbyid()!=null){
            subcontent = new HashMap<>();
            BtCtWeihubaoyangEntity btCtWeihubaoyangEntity = btCtWeihubaoyangService.selectById(pdbean.getWhbyid());
            if(btCtWeihubaoyangEntity!=null){
                if(btCtWeihubaoyangEntity.getFenleiid()!=null && btCtWeihubaoyangEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtWeihubaoyangEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtWeihubaoyangDetailEntity btCtWeihubaoyangDetailEntity = btCtWeihubaoyangDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtWeihubaoyangDetailEntity.getName());
                        subsubcont.put("content",btCtWeihubaoyangDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","维护保养");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("weihubaoyang",subcontent);
                }else{
                    if(btCtWeihubaoyangEntity.getSummary()!=null&& btCtWeihubaoyangEntity.getSummary()!=""){
                        subcontent.put("name","维护保养");
                        subcontent.put("content",btCtWeihubaoyangEntity.getSummary());
                        backdata.put("weihubaoyang",subcontent);
                    }
                }
            }
        }
        //故障排除
        if(pdbean.getGzpcid()!=null){
            subcontent = new HashMap<>();
            BtCtGuozhangpaichuEntity btCtGuozhangpaichuEntity = btCtGuozhangpaichuService.selectById(pdbean.getGzpcid());
            if(btCtGuozhangpaichuEntity!=null){
                if(btCtGuozhangpaichuEntity.getFenleiid()!=null && btCtGuozhangpaichuEntity.getFenleiid()!=""){
                    subfenleilist = new ArrayList<>();
                    String[] tstrarr = btCtGuozhangpaichuEntity.getFenleiid().split(";");
                    for(int i=0; i<tstrarr.length;i++){
                        subsubcont = new HashMap<>();
                        BtCtGuozhangpaichuDetailEntity btCtGuozhangpaichuDetailEntity = btCtGuozhangpaichuDetailService.selectById(tstrarr[i]);
                        subsubcont.put("name",btCtGuozhangpaichuDetailEntity.getName());
                        subsubcont.put("content",btCtGuozhangpaichuDetailEntity.getContent());
                        subfenleilist.add(subsubcont);
                    }
                    subcontent.put("name","故障排除");
                    subcontent.put("sublist",subfenleilist);
                    backdata.put("guzhangpaicu",subcontent);
                }else{
                    if(btCtGuozhangpaichuEntity.getSummary()!=null&& btCtGuozhangpaichuEntity.getSummary()!=""){
                        subcontent.put("name","故障排除");
                        subcontent.put("content",btCtGuozhangpaichuEntity.getSummary());
                        backdata.put("guzhangpaicu",subcontent);
                    }
                }
            }
        }
        pdbean.setLlcs(pdbean.getLlcs()+1);
        btCtBaseService.insertOrUpdate(pdbean);
        return backdata;
    }
}
